package com.takeaway.servlet.filter;

import com.takeaway.pojo.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 后台赖骏
 */
@WebFilter(urlPatterns = {"/jsp/Recharge.jsp","/user/checkCoupon"
        ,"/user/addBook","/user/checkUserBook","/user/addMsg"
        ,"/user/checkRealCoupon","/user/addCoupon","/user/addCollect"
        ,"/user/upLoadImg","/user/checkCollect","/user/addCartBook"})
public class UserFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig){

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        User user = (User)req.getSession().getAttribute("user");
        System.out.println("用户过滤执行");
        if (user==null){
            req.getRequestDispatcher("/jsp/login.jsp").forward(req,resp);
        }
        else {
            filterChain.doFilter(req, resp);
        }
    }

    @Override
    public void destroy() {

    }
}
